/*
 * Copyright [2011] [Ambud Sharma/ODep Monitor]

   Licensed under the Apache License, Version 2.0 (the "License");

   you may not use this file except in compliance with the License.

   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software

   distributed under the License is distributed on an "AS IS" BASIS,

   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

   See the License for the specific language governing permissions and

   limitations under the License.
 */
package edu.uwsp.orion.odepm.main.server;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

public class FileUpload extends HttpServlet {
	
	private static final String TMP_DIR_PATH = new File("").getAbsolutePath();
	private File tmpDir;
	private static final String DESTINATION_DIR_PATH ="/files.txt";
	private File destinationDir;
  	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
	//	super.doPost(req, resp);
		System.out.println("hello:"+TMP_DIR_PATH+DESTINATION_DIR_PATH+"  :");
		HttpSession ssn=req.getSession();
		
		ServletFileUpload upload=new ServletFileUpload();
		try{
            FileItemIterator iter = upload.getItemIterator(req);
            if(iter==null){
            	System.err.println("null");
            }
            while (iter.hasNext()) {
                FileItemStream item = iter.next();

                String name = item.getFieldName();
                InputStream stream = item.openStream();
                System.out.println("hello:"+TMP_DIR_PATH+DESTINATION_DIR_PATH+"  :"+name);
                ssn.setAttribute("jarLocation", TMP_DIR_PATH+name);
                DataOutputStream dataOutput=new DataOutputStream(new FileOutputStream(TMP_DIR_PATH+"/"+name));
                int len;
                byte[] buffer = new byte[8192];
                while ((len = stream.read(buffer, 0, buffer.length)) != -1) {
                    dataOutput.write(buffer,0,len);
                }
                
                dataOutput.flush();
                dataOutput.close();
            }
        }
        catch(Exception e){
            e.printStackTrace();
        }
		
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.doGet(req, resp);
		doPost(req, resp);
	}
}
